package MyThread;

/**
 * volatile 两个线程依次打印 奇数和偶数
 */
public class Thread_volatile {
    private static volatile boolean flag4Volatile = true;
    private static int index = 1;
    private static int end = 60;
    private static volatile int flag3 = 0;

    public static void testVolatile() throws InterruptedException {
        Thread t1 = new Thread( () -> {
            while (index <= end) {
                if (flag4Volatile) {
                    System.out.println((Thread.currentThread().getName() + ": " + index++));
                    flag4Volatile = false;
                }
            }
        });

        Thread t2 = new Thread( () -> {
            while (index <= end) {
                if (!flag4Volatile) {
                    System.out.println((Thread.currentThread().getName() + ": " + index++));
                    flag4Volatile = true;
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }

    /**
     * volatile 三个线程依次打印
     */

    public static void testVolatile3() throws InterruptedException {
        Thread t0 = new Thread(() ->{
            while (index <= end){
                if(flag3 == 0){
                    System.out.println((Thread.currentThread().getName() + ": " + index++));
                    flag3 = (flag3 + 1) % 3;
                }
            }
        });
        Thread t1 = new Thread(() ->{
            while (index <= end){
                if(flag3 == 1){
                    System.out.println((Thread.currentThread().getName() + ": " + index++));
                    flag3 = (flag3 + 1) % 3;
                }
            }
        });
        Thread t2 = new Thread(() ->{
            while (index <= end){
                if(flag3 == 2){
                    System.out.println((Thread.currentThread().getName() + ": " + index++));
                    flag3 = (flag3 + 1) % 3;
                }
            }
        });
        t0.start();
        t1.start();
        t2.start();
        t0.join();
        t1.join();
        t2.join();
    }

    /**
     * volatile 三个线程依次打印 5 个数
     */
    public static void testVolatile3_5() throws InterruptedException {
        Thread t0 = new Thread(() ->{
            while (index <= end){
                if(flag3 == 0){
                    System.out.print(Thread.currentThread().getName() + ":");
                    for(int i= 0; i<= 4; i++){
                        System.out.print(" " + index++);
                    }
                    System.out.println();
                    flag3 = (flag3 + 1) % 3;
                }
            }
        });
        Thread t1 = new Thread(() ->{
            while (index <= end){
                if(flag3 == 1){
                    System.out.print(Thread.currentThread().getName() + ":");
                    for(int i= 0; i<= 4; i++){
                        System.out.print(" " + index++);
                    }
                    System.out.println();
                    flag3 = (flag3 + 1) % 3;
                }
            }
        });
        Thread t2 = new Thread(() ->{
            while (index <= end){
                if(flag3 == 2){
                    System.out.print(Thread.currentThread().getName() + ":");
                    for(int i= 0; i<= 4; i++){
                        System.out.print(" " + index++);
                    }
                    System.out.println();
                    flag3 = (flag3 + 1) % 3;
                }
            }
        });
        t0.start();
        t1.start();
        t2.start();
        t0.join();
        t1.join();
        t2.join();
    }

    public static void main(String[] args) throws Exception {
        testVolatile3_5();
    }

}
